import request from "@/utils/request";
import httpRequest from "@/utils/requestUtil";
import { IObject } from "@/api/common/common";
import { ContractItem } from "./contract.api";

const CONTRACT_DESIGN_BASE_URL = "/business/contractDesign";

const ContractDesignAPI = {
  /** 获取分页数据 */
  getPage(queryParams?: ContractDesignPageQuery) {
    return httpRequest.get<PageResult<ContractDesignPageVO[]>>(
      `${CONTRACT_DESIGN_BASE_URL}/loadPage`,
      queryParams
    );
  },

  /** 获取分配给自己的数据 */
  getOwnPage(queryParams?: ContractDesignPageQuery) {
    return httpRequest.get<PageResult<ContractDesignPageVO[]>>(
      `${CONTRACT_DESIGN_BASE_URL}/loadOwn`,
      queryParams
    );
  },

  /**
   * 派单
   * @param contractId 合同id
   * @param designer 设计师
   * @returns
   */
  dispatch(contractId: string, designer: string) {
    return httpRequest.get(
      `${CONTRACT_DESIGN_BASE_URL}/dispatch?contractId=${contractId}&designer=${designer}`
    );
  },

  /**
   * 确认
   * @param contractId 合同id
   * @returns
   */
  confirm(params: { contractId: string; customerSignId: string }) {
    return request<any, boolean>({
      url: `${CONTRACT_DESIGN_BASE_URL}/confirm`,
      method: "get",
      params,
    });
  },

  /**
   * 设计合同清单，这里只能修改合同项
   * @param items
   * @returns
   */
  reviewContract(data: ContractDesignForm) {
    return request<any, boolean>({
      url: `${CONTRACT_DESIGN_BASE_URL}/reviewContract`,
      method: "post",
      data,
    });
  },

  download(contractId: string) {
    return request<any, Blob>({
      url: `${CONTRACT_DESIGN_BASE_URL}/download?contractId=${contractId}`,
      method: "get",
      responseType: "blob",
    });
  },

  loadById(id: string) {
    return request<any, ContractDesignPageVO>({
      url: `${CONTRACT_DESIGN_BASE_URL}/load/${id}`,
      method: "get",
    });
  },
};

export default ContractDesignAPI;

/** 分页查询参数 */
export interface ContractDesignPageQuery extends PageQuery {
  /** 设计师id */
  designer?: string;
}

/** 分页对象 */
export interface ContractDesignPageVO {
  id?: string;
  contractId?: string;
  contractName?: string;
  designer?: string;
  designerName?: string;
  dispatchTime?: string;
  confirmTime?: string;
  status: string;
}

export interface ContractDesignForm {
  /** 合同id */
  id?: string;
  items?: ContractItem[];
}

export enum ContractDesignStatus {
  WAIT_DISPATCH = "WAIT_DISPATCH",
  DESIGNING = "DESIGNING",
  CONFIRM = "CONFIRM",
}

export const ContractDesign_Status_Label: IObject = {
  [ContractDesignStatus.WAIT_DISPATCH]: "待派单",
  [ContractDesignStatus.DESIGNING]: "设计中",
  [ContractDesignStatus.CONFIRM]: "已确认",
};
